//
//  Utils.m
//  GTShareSDK
//
//  Created by yate1996 on 15/7/18.
//  Copyright (c) 2015年 yate1996. All rights reserved.
//

#import "Utils.h"

@interface Utils ()
/**
 *  视图
 */
@property (nonatomic, strong) UIView *coverView;

/**
 *  面板
 */
@property (nonatomic, strong) UIView *panelView;

/**
 *  加载视图
 */
@property (nonatomic, strong) UIActivityIndicatorView *loadingView;



@end

@implementation Utils

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
}
-(void)setupView
{
    self.coverView = [[UIView alloc] initWithFrame:self.view.bounds];
    self.coverView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:self.coverView];
    
    //加载等待视图
    self.panelView = [[UIView alloc] initWithFrame:self.view.bounds];
    self.panelView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    self.panelView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    
    self.loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    self.loadingView.frame = CGRectMake((self.view.frame.size.width - self.loadingView.frame.size.width) / 2, (self.view.frame.size.height - self.loadingView.frame.size.height) / 2, self.loadingView.frame.size.width, self.loadingView.frame.size.height);
    self.loadingView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
    [self.panelView addSubview:self.loadingView];

    
}


- (void)switchChangeHandler:(UISwitch *)sender
{
    
    //新浪微博
    if (sender.on)
    {
        //授权
        [ShareSDK authorize:SSDKPlatformTypeSinaWeibo settings:nil onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
            
            
        }];
    }
    else
    {
        //取消授权
        [ShareSDK cancelAuthorize:SSDKPlatformTypeSinaWeibo];
    }
    
}

#pragma mark - UIpopverViewDataSource


/**
 *  显示加载动画
 *
 *  @param flag YES 显示，NO 不显示
 */
- (void)showLoadingView:(BOOL)flag
{
    if (flag)
    {
        [self.view addSubview:self.panelView];
        [self.loadingView startAnimating];
    }
    else
    {
        [self.panelView removeFromSuperview];
    }
}


#pragma mark 显示分享菜单

/**
 *  显示分享菜单
 *
 *  @param view 容器视图
 */
- (void)showShareActionSheet:(UIView *)view withShareParamsByText:(NSString *)text
                      images:(NSArray *)images
                         url:(NSURL *)url
                       title:(NSString *)title
                        type:(SSDKContentType)type
{
    
    __weak Utils *theController = self;
        //1、创建分享参数
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:text
                                     images:nil
                                        url:url
                                      title:text
                                       type:SSDKContentTypeText];
    [shareParams SSDKSetupSinaWeiboShareParamsByText:text
                                               title:text
                                               image:nil
                                                 url:url
                                            latitude:-1
                                           longitude:-1
                                            objectID:nil
                                                type:SSDKContentTypeText];
    [shareParams SSDKSetupWeChatParamsByText:text
                                       title:text
                                         url:url
                                  thumbImage:nil
                                       image:nil
                                musicFileURL:nil
                                     extInfo:nil
                                    fileData:nil
                                emoticonData:nil
                                        type:SSDKContentTypeText
                          forPlatformSubType:SSDKPlatformSubTypeWechatSession|SSDKPlatformSubTypeWechatTimeline|SSDKPlatformSubTypeWechatFav];
    
    //1.1、QQ空间不支持SSDKContentTypeImage这种类型，所以需要定制下。
    [shareParams SSDKSetupQQParamsByText:@"QQ空间分享"
                                   title:@"分享标题"
                                     url:[NSURL URLWithString:@"http://mob.com"]
                              thumbImage:nil
                                   image:[UIImage imageNamed:@"shareImg.png"]
                                    type:SSDKContentTypeWebPage
                      forPlatformSubType:SSDKPlatformSubTypeQZone];
    [SSUIShareActionSheetStyle setPageIndicatorTintColor:[UIColor colorWithRed:62/255.0 green:62/255.0 blue:62/255.0 alpha:1.0]];
    
    //2、分享
    [ShareSDK showShareActionSheet:view
                             items:nil
                       shareParams:shareParams
               onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                   
                   switch (state) {
                           
                       case SSDKResponseStateBegin:
                       {
                           [theController showLoadingView:YES];
                           break;
                       }
                       case SSDKResponseStateSuccess:
                       {
                           UIView *view = [[[self.navigationController.childViewControllers firstObject] subviews] firstObject];
                           [MBProgressHUD showSuccess:@"分享成功" toView:view];
                           break;
                           
                       }
                       case SSDKResponseStateFail:
                       {
                           UIView *view = [[[self.navigationController.childViewControllers firstObject] subviews] firstObject];
                           
                           [MBProgressHUD showError:@"分享失败，请稍后再试" toView:view];
                           NSLog(@"%@",error);
                           break;
                       }
                       default:
                           break;
                   }
                   if (state != SSDKResponseStateBegin)
                   {
                       [theController showLoadingView:NO];
                   }
                   
               }];
}

@end
